﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 网站导航仓储
/// </summary>
public class MdNavigationRep : BaseRep<MdNavigationModel>
{
    private static string sqlFields = string.Format(@"
        `navigation_id`,
        `name`,
        `page_path`,
        `link`,
        `is_external_link`,
        `sort`,
        `status`,
        `del_flag`,
        `create_by`,
        `create_time`,
        `update_by`,
        `update_time`
    ");

    /// <summary>
    /// 通过编号获取导航信息
    /// </summary>
    /// <param name="id">产品编号</param>
    /// <returns>MdProductModel</returns>
    public MdNavigationModel? GetMdNavigationById(long id)
    {
        string sqlWhere = string.Format(@" and `navigation_id` = @NavigationId ");

        return QuerySingle(sqlFields, sqlWhere, new { NavigationId = id });
    }

    /// <summary>
    /// 获取网站导航列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒叙</param>
    /// <returns>IEnumerable<MdNavigationModel></returns>
    public IEnumerable<MdNavigationModel> GetMdNavigationList(MdNavigationModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Name))
        {
            sqlWhere += " and `name` like concat('%', @Name, '%') ";
        }
        if(!string.IsNullOrWhiteSpace(model.PagePath))
        {
            sqlWhere += " and `page_path` like concat('%', @PagePath, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Link))
        {
            sqlWhere += " and `link` like concat('%', @Link, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.IsExternalLink))
        {
            sqlWhere += " and `is_external_link` = @IsExternalLink ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }
        sqlWhere += " and del_flag = '0' ";
        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取网站导航列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<MdNavigationModel></returns>
    public Page<MdNavigationModel> GetMdNavigationPageList(MdNavigationModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (!string.IsNullOrWhiteSpace(model.Name))
        {
            sqlWhere += " and `name` like concat('%', @Name, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.PagePath))
        {
            sqlWhere += " and `page_path` like concat('%', @PagePath, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Link))
        {
            sqlWhere += " and `link` like concat('%', @Link, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.IsExternalLink))
        {
            sqlWhere += " and `is_external_link` = @IsExternalLink ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }
        sqlWhere += " and del_flag = '0' ";
        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过编号更新网站导航
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>long</returns>
    public long UpdateMdNavigationById(MdNavigationModel model)
    {
        var sqlSet = string.Empty;

        #region ===set===
        if (!string.IsNullOrWhiteSpace(model.Name))
        {
            sqlSet += " `name` = @Name, ";
        }

        if (!string.IsNullOrWhiteSpace(model.PagePath))
        {
            sqlSet += " `page_path` = @PagePath, ";
        }

        if(!string.IsNullOrWhiteSpace(model.Link))
        {
            sqlSet += " `link` = @Link, ";
        }

        if (!string.IsNullOrWhiteSpace(model.IsExternalLink))
        {
            sqlSet += " `is_external_link` = @IsExternalLink, ";
        }

        if (model.Sort > -1)
        {
            sqlSet += " `sort` = @Sort, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `del_flag` = '0' and `navigation_id` = @NavigationId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过编号删除网站导航
    /// </summary>
    /// <param name="ids">编号多个逗号分隔</param>
    /// <returns>int</returns>
    public int DeleteMdNavigationById(string ids)
    {
        string sqlWhere = " and `navigation_id` in @NavigationIds ";

        return Delete(sqlWhere, new { NavigationIds = ids.Split(',').ToArray() });
    }
}
